package com.ld.security.auth.common.base.aop;

import com.ld.security.auth.common.impl.ApiLogQueueImpl;
import com.ld.security.auth.common.po.ApiLogPo;
import com.ld.security.common.core.anno.ApiLog;
import com.ld.security.common.core.util.AopUtil;
import com.ld.security.common.core.util.TemplateUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

@Component
@Aspect
@Order(5)
public class ApiLogAop {


    @Autowired
    private ApiLogQueueImpl apiLogPoSuperQueue;

    @Pointcut("@annotation(com.ld.security.common.core.anno.ApiLog)")
    public void aop() {

    }

   @Around("aop()")
    public Object doAroundAdvice(ProceedingJoinPoint pjp) throws Throwable{
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        Method method = ms.getMethod();
        ApiLog apiMapping =  AnnotationUtils.findAnnotation(method, ApiLog.class);
        Map params = AopUtil.getParams(pjp);

        String label = TemplateUtil.renderString( apiMapping.label(),params ) ;
        String logType = apiMapping.logType();
        String content =TemplateUtil.renderString(  apiMapping.value(),params);
        ApiLogPo po = new ApiLogPo();
        po.setLabel(label);
        po.setLogType(logType);
        po.setContent(content);
        apiLogPoSuperQueue.push(po);
        return pjp.proceed();
    }



}
